﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TLECDataLayer
{
    public class Events
    {
        public static void CreateEvent(string title, int locId, int catId, decimal price, DateTime date)
        {
            if (!string.IsNullOrEmpty(title) || date == null || price < 0)
            {
                TLECDataDataContext context = new TLECDataDataContext();

                var loc =
                    (from l in context.Locations
                     where l.LocationId == locId
                     select l).FirstOrDefault();

                var cat =
                    (from c in context.Categories
                     where c.CategoryId == catId
                     select c).FirstOrDefault();

                if (loc != null && cat != null)
                {
                    Event e = new Event()
                    {
                        Title = title,
                        CategoryId = cat.CategoryId,
                        LocationId = loc.LocationId,
                        DateAndTime = date,
                        Price = price
                    };

                    context.Events.InsertOnSubmit(e);
                    context.SubmitChanges();
                }
                else
                {
                    throw new ArgumentException("Invalid categorie id or location id");
                }

            }
            else
            {
                throw new ArgumentException("Invalid event arguments");
            }

        }

        public static void UpdateEvent(int eveId, string title, int locId, int catId, decimal price, DateTime date)
        {
            if (!string.IsNullOrEmpty(title) || date == null || price < 0)
            {
                TLECDataDataContext context = new TLECDataDataContext();

                var eve =
                    (from e in context.Events
                     where e.EventId == eveId
                     select e).FirstOrDefault();

                var loc =
                   (from l in context.Locations
                    where l.LocationId == locId
                    select l).FirstOrDefault();

                var cat =
                    (from c in context.Categories
                     where c.CategoryId == catId
                     select c).FirstOrDefault();

                if (loc != null && cat != null && eve != null)
                {
                    eve.Title = title;
                    eve.CategoryId = catId;
                    eve.LocationId = locId;
                    eve.DateAndTime = date;
                    eve.Price = price;
                    context.SubmitChanges();
                }
                else
                {
                    throw new ArgumentException("Invalid location id or categorie id or event id");
                }
            }
            else
            {
                throw new ArgumentException("Invalid event arguments");
            }
        }

        public static void DeleteEvent(int eveId)
        {
            TLECDataDataContext context = new TLECDataDataContext();

            var res =
                (from l in context.Events
                 where l.EventId == eveId
                 select l).FirstOrDefault();

            if (res != null)
            {
                var resers =
                    (from r in context.Reservations
                     where r.EventId == eveId
                     select r).FirstOrDefault();

                if (resers == null)
                {
                    context.Events.DeleteOnSubmit(res);
                    context.SubmitChanges();
                }

            }
            else
            {
                throw new ArgumentException("Invalid event id");
            }
        }

        public static IQueryable<Event> GetAllEvents()
        {
            TLECDataDataContext context = new TLECDataDataContext();

            return context.Events;
        }

        public static IQueryable<Event> GetAllUpcomingEvents()
        {
            TLECDataDataContext context = new TLECDataDataContext();

            var res =
                from e in context.Events
                where e.DateAndTime.Date >= DateTime.Now.AddDays(3).Date
                orderby e.DateAndTime
                select e;

            return res;
        }

        public static IQueryable<Event> GetAllUpcomingEventsByCategory(int catId)
        {
            TLECDataDataContext context = new TLECDataDataContext();

            var res =
                from e in context.Events
                where e.DateAndTime.Date >= DateTime.Now.AddDays(3).Date && e.CategoryId == catId
                orderby e.DateAndTime
                select e;

            return res;
        }

        public static IQueryable<Event> GetAllEventsByLocation(int locId)
        {
            TLECDataDataContext context = new TLECDataDataContext();

            var events =
                    from e in context.Events
                    where e.LocationId == locId
                    select e;

            return events;
        }

        public static IQueryable<Event> GetAllEventsByCategory(int catId)
        {
            TLECDataDataContext context = new TLECDataDataContext();

            var events =
                from e in context.Events
                where e.CategoryId == catId
                select e;

            return events;
        }

        public static Event GetEventById(int eveId)
        {
            TLECDataDataContext context = new TLECDataDataContext();

            var eve =
                (from l in context.Events
                 where l.EventId == eveId
                 select l).FirstOrDefault();

            return eve;
        }
    }
}
